package com.tanhua.server.service;

import com.tanhua.autoconfig.templates.OssTemplate;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VoiceApi;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.db.Voice;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.VoiceVo;
import com.tanhua.server.exceptions.BusinessException;
import com.tanhua.server.intercepror.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PeachblossomService {

    @Autowired
    private OssTemplate ossTemplate;

    @DubboReference
    private VoiceApi voiceApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    //上传语音
    public void peachblossom(MultipartFile soundFile) throws IOException {
        Long userId = UserHolder.getUserId();
        String url = ossTemplate.upload(soundFile.getOriginalFilename(), soundFile.getInputStream());
        voiceApi.save(userId, url);
    }


    //取出一条没有用过的语音
    public VoiceVo peachblossom() {
        //1.判断用户是否还有获取语音次数
        Long userId = UserHolder.getUserId();
        UserInfo userInfo = userInfoApi.findById(userId);//当前登入用户
        Integer remainingTimes = userInfo.getRemainingTimes();//remainingTimes;//语音拾取次数
        Integer voiceTime = userInfo.getVoiceTime();//获取 当前次数的有效时间
        //获取当前时间
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        Integer nowTime = Integer.valueOf(date);
        //判断时间是不是今天
        if (voiceTime != nowTime) {
            //不等于代表次数要重置，并将时间改为今天
            remainingTimes = 10;
            voiceTime = nowTime;
            userInfo.setVoiceTime(voiceTime);
        }
        //等于将次数-1 并判断是不是还有次数
        remainingTimes -= 1;
        if (remainingTimes < 0) {
            //小于0代表没次数
            throw new BusinessException(ErrorResult.myError("没次数了哦"));
        }
        userInfo.setRemainingTimes(remainingTimes);
        userInfoApi.update(userInfo);//将最新时间和次数
        //有次数去查询出所有的 语音
        List<Voice> voices = voiceApi.findAll();
        for (Voice voice : voices) {
            Integer mark = voice.getMark();
            if (mark == 1 || userId != voice.getUserId()) {//代表语音没被用过,不是自己发的语音
                //分装vo返回并讲这条设置为已用过
                VoiceVo vo = new VoiceVo();
                //获取语音用户
                UserInfo voiceUser = userInfoApi.findById(voice.getUserId());
                vo.setId(voice.getUserId());
                vo.setAge(voiceUser.getAge());
                vo.setAvatar(voiceUser.getAvatar());
                vo.setGender(voiceUser.getGender());
                vo.setSoundUrl(voice.getSoundUrl());
                vo.setNickname(voiceUser.getNickname());
                vo.setRemainingTimes(remainingTimes);//当前用户的次数
                voice.setMark(2);
                voiceApi.update(voice);
                return vo;
            }
        }
        throw new BusinessException(ErrorResult.myError("没有语音可以获取"));
    }
}
